package com.yupi.yangoj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yangoj.common.BaseResponse;
import com.yupi.yangoj.common.ErrorCode;
import com.yupi.yangoj.common.ResultUtils;
import com.yupi.yangoj.exception.BusinessException;
import com.yupi.yangoj.model.dto.questionsubmit.QuestionSubmitAddRequest;
import com.yupi.yangoj.model.dto.questionsubmit.QuestionSumitQueryRequest;
import com.yupi.yangoj.model.entity.Question;
import com.yupi.yangoj.model.entity.QuestionSubmit;
import com.yupi.yangoj.model.entity.User;
import com.yupi.yangoj.model.vo.QuestionSubmitVO;
import com.yupi.yangoj.model.vo.QuestionVO;
import com.yupi.yangoj.service.QuestionSubmitService;
import com.yupi.yangoj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 题目提交接口
 */
@RestController
@RequestMapping("/question_submit")
@Slf4j
public class QuestionSubmitController {

    @Resource
    private QuestionSubmitService questionSubmitService;

    @Resource
    private UserService userService;

    /**
     * 用户提交题目
     *
     * @param questionSubmitAddRequest
     * @param request
     * @return 提交记录的id
     */
    @PostMapping("/")
    public BaseResponse<Long> doSubmitQuestion(@RequestBody QuestionSubmitAddRequest questionSubmitAddRequest,
                                               HttpServletRequest request) {
        //判断参数
        if (questionSubmitAddRequest == null || questionSubmitAddRequest.getQuestionId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取登录用户
        final User loginUser = userService.getLoginUser(request);

        //提交题目，并获取提交id
        long questionSubmitId = questionSubmitService.doQuestionSubmit(questionSubmitAddRequest, loginUser);
        return ResultUtils.success(questionSubmitId);
    }


    /**
     * 分页获取题目提交列表（除了管理员外，其他用户只能查看除了提交代码，答案外的提交信息）
     *
     * @param questionSumitQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<QuestionSubmitVO>> listQuestionSubmitByPage(@RequestBody QuestionSumitQueryRequest questionSumitQueryRequest,
                                                                         HttpServletRequest request) {

        long current = questionSumitQueryRequest.getCurrent();
        long pageSize = questionSumitQueryRequest.getPageSize();
        //分页查询数据库中原始的题目提交数据
        Page<QuestionSubmit> questionSubmitPage = questionSubmitService.page(new Page<>(current, pageSize),
                questionSubmitService.getQueryWrapper(questionSumitQueryRequest));

        //进行脱敏处理，然后返回vo数据
        final User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(questionSubmitService.getQuestionSubmitVOPage(questionSubmitPage, loginUser));
    }


    /**
     * 根据 题目提交id 获取脱敏后的数据（仅供该题目的提交者或管理员才可以查看）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<QuestionSubmitVO> getQuestionSubmitVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //查找题目数据
        QuestionSubmit questionSubmit = questionSubmitService.getById(id);
        if (questionSubmit == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        //将题目数据进行脱敏后返回
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        //仅本人和管理员可以调用，否则返回null
        if (!Objects.equals(userId, questionSubmit.getUserId()) && !userService.isAdmin(loginUser)) {
            return ResultUtils.success(null);
        }

        return ResultUtils.success(questionSubmitService.getQuestionSubmitDetailVO(questionSubmit, loginUser));
    }


}
